package com.m3958.firstgwt.server.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import com.m3958.firstgwt.server.types.JsonExcludeFields;
import com.m3958.firstgwt.server.utils.JsonlibDateValueProssor;

@Entity
@Table(name="VOTE_HIT")
@AttributeOverride(name="id",column=@Column(nullable = false,name="VOTE_HIT_ID"))
@NamedQueries({
	@NamedQuery(name="findVoteHitsByVotes",
			query="SELECT COUNT(DISTINCT h) FROM VoteHit AS h WHERE ?1 MEMBER OF h.votes AND ?2 MEMBER OF h.votes")
})
public class VoteHit extends BaseModel<VoteHit>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static class NamedQueries{
		public final static String FIND_VOTEHITS_BY_VOTES = "findVoteHitsByVotes"; 
	}
	
	
	@Override
	public JSONObject toJson(){
		JsonConfig jc = new JsonConfig();
		jc.registerJsonValueProcessor(Date.class, injector.getInstance(JsonlibDateValueProssor.class));
		jc.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		jc.setExcludes(JsonExcludeFields.VOTE_HIT_EXCLUDES);
		JSONObject jo =  JSONObject.fromObject(this, jc);
		return jo;
	}
	

	private String referer;
	
	private String ip;
	
	@ManyToMany(mappedBy="voteHits")
	private List<Vote> votes = new ArrayList<Vote>();

	public String getReferer() {
		return referer;
	}

	public void setReferer(String referer) {
		this.referer = referer;
	}

	public void setVotes(List<Vote> votes) {
		this.votes = votes;
	}

	public List<Vote> getVotes() {
		return votes;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getIp() {
		return ip;
	}

	@Override
	public String[] getJsonExcludes() {
		// TODO Auto-generated method stub
		return null;
	}
}
